/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.firewall;

import nfse.NetFSERecord;
import nfse.NetFSEUtil;
import nfse.gui.record.FlexRecord;
import nfse.gui.record.FlexRecordField;

import java.sql.Timestamp;

public class FirewallRecord extends NetFSERecord {

    public int proto = -1;

    public String appProto = "";

    public String type = "";

    public String subType = "";

    public String resource = "";

    public String format = "";

    public String fwType = "";

    public String user = "";

    public String priority = "";

    public int dstPort = -1;

    public int srcPort = -1;

    //public long srcIP = -1;

    public long dstAddr = -1;

    //public String srcAddr = "";

    public String srcName = "";

    public String dstQuad = "";

    public String dstName = "";

    public long srcBytes = 0;

    public int srcPackets = 0;

    public long dstBytes = 0;

    public int dstPackets = 0;

    public int httpStatus = 0;

    public int rule = 0;

    public int eventCode = 0;

    public int eventType = 0;

    public String message = "";
    public String request= "";

    public int sensor = -1;

    public float duration = (float) 0.0;

    public FirewallRecord() {

    }
    
    public static void main(String[] args) {
        FirewallRecord rec = new FirewallRecord();
        rec.parse(args, rec);
    }

    public FirewallRecord(int sensor) {
        this.sensor = sensor;
    }
    
    public void setDstIP(long dstIP) {
        this.dstAddr = dstIP;
        this.dstQuad = NetFSEUtil.convertLongToIP(dstIP);
    }
    
    
    
    
    public NetFSERecord generate(String line, int sensor) {
        FirewallRecord rec = new FirewallRecord(sensor);
        // System.out.println(line);
        rec.setInitialString(line);
        line = line.trim();
        String[] tokens = line.split(",");
        rec.resource = tokens[1];
        rec.format = tokens[2];
        rec.fwType = tokens[3];
        rec.user = tokens[4];
        rec.appProto = tokens[6];

        rec.priority = tokens[8];

        rec.type = tokens[9];
        rec.subType = tokens[10];
        rec.setSrcAddr(tokens[11]);
        try {
            rec.setSrcIP(NetFSEUtil.convertIPtoLong(rec.getSrcAddr()));
        } catch (Exception e) {
            // e.printStackTrace();
            rec.setSrcIP(0);
        }
        rec.srcName = tokens[12];
        try {
            rec.srcPort = Integer.parseInt(tokens[13]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.srcPort = 0;
        }

        rec.dstQuad = tokens[14];
        try {
            rec.dstAddr = NetFSEUtil.convertIPtoLong(rec.dstQuad);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.dstAddr = 0;
        }
        rec.dstName = tokens[15];
        try {
            rec.dstPort = Integer.parseInt(tokens[16]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.dstPort = 0;
        }
        try {
            rec.duration = Float.parseFloat(tokens[17]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.duration = (float) 0.0;
        }

        try {
            rec.srcBytes = Long.parseLong(tokens[22]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.srcBytes = 0;
        }

        try {
            rec.dstBytes = Long.parseLong(tokens[23]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.dstBytes = 0;
        }

        try {
            rec.srcPackets = Integer.parseInt(tokens[24]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.srcPackets = 0;
        }

        try {
            rec.dstPackets = Integer.parseInt(tokens[25]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.dstPackets = 0;
        }

        try {
            rec.httpStatus = Integer.parseInt(tokens[26]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.httpStatus = 0;
        }

        try {
            rec.rule = Integer.parseInt(tokens[27]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.rule = 0;
        }

        try {
            rec.eventCode = Integer.parseInt(tokens[28]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.eventCode = 0;
        }

        try {
            rec.eventType = Integer.parseInt(tokens[29]);
        } catch (Exception e) {
            // e.printStackTrace();
            rec.eventType = 0;
        }
        try {
            rec.message = tokens[30];
        } catch (Exception e) {

        }
        try {
            rec.request = tokens[18];
        } catch (Exception e) {

        }
        try {
            rec.setStartTS(new Timestamp(Long.parseLong(tokens[43]) * (long) 1000));
        } catch (Exception e) {
            rec.setStartTS(new Timestamp(System.currentTimeMillis()));
        }

        return rec;
    }
    
    public String getCreateFieldTableSQL(String table) {
        StringBuffer temp = new StringBuffer("create table " + table + " (ID bigint, ");
        temp.append("srcip inet,");
        temp.append("srcport int,");
        temp.append("dstip inet,");
        temp.append("dstport int,");
        temp.append("startts timestamp,");
        temp.append("duration float,");
        temp.append("srcname varchar(100),");
        temp.append("srcpkts bigint,");
        temp.append("srcbytes bigint,");
        temp.append("dstname varchar(100),");
        temp.append("dstpkts bigint,");
        temp.append("dstbytes bigint,");
        temp.append("priority varchar(50),");
        temp.append("eventtype int,");
        temp.append("eventcode int,");
        temp.append("rule int,");
        temp.append("httpstatus int,");
        temp.append("username varchar(50),");
        temp.append("resource varchar(20),");
        temp.append("format varchar(30),");
        temp.append("fwtype varchar(50),");
        temp.append("appproto varchar(15),");
        temp.append("type varchar(10),");
        temp.append("subtype varchar(10),");
        temp.append("request varchar(255),");
        temp.append("message varchar(255)");
        temp.append(")");
        return temp.toString();
    }
    
    public FlexRecord getFlexRecord() {
        FlexRecord temp = new FlexRecord();
        temp.values = new String[15];
        int idx = -1;
        temp.values[++idx] = this.getTimestamp().toString();
        temp.values[++idx] = this.duration + "";
        temp.values[++idx] = this.getSrcAddr();
        temp.values[++idx] = this.srcPort + "";
        temp.values[++idx] = this.srcPackets + "";
        temp.values[++idx] = this.srcBytes + "";
        temp.values[++idx] = this.dstQuad;
        temp.values[++idx] = this.dstPort + "";
        temp.values[++idx] = this.dstPackets + "";
        temp.values[++idx] = this.dstBytes + "";
        temp.values[++idx] = this.appProto;
        temp.values[++idx] = this.priority;
        temp.values[++idx] = this.type;
        temp.values[++idx] = this.subType;
        temp.values[++idx] = this.message;
        return temp;
    }
    
    public FlexRecordField[] getFlexRecordFields() {
        FlexRecordField[] temp = new FlexRecordField[15];
        int idx = -1;
        temp[++idx] = new FlexRecordField("Timestamp", "startts", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_TIMESTAMP);
        temp[++idx] = new FlexRecordField("Duration", "duration", FlexRecordField.TYPE_FLOAT, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Src IP", "srcip", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_IP_ADDR);
        temp[++idx] = new FlexRecordField("Src Port", "srcport", FlexRecordField.TYPE_INT, FlexRecordField.SUB_TYPE_PORT);
        temp[++idx] = new FlexRecordField("Src Pkts", "srcpkts", FlexRecordField.TYPE_LONG, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Src Bytes", "srcbytes", FlexRecordField.TYPE_LONG, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Dst IP", "dstip", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_IP_ADDR);
        temp[++idx] = new FlexRecordField("Dst Port", "dstport", FlexRecordField.TYPE_INT, FlexRecordField.SUB_TYPE_PORT);
        temp[++idx] = new FlexRecordField("Dst Pkts", "dstpkts", FlexRecordField.TYPE_LONG, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Dst Bytes", "dstbytes", FlexRecordField.TYPE_LONG, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("App Proto", "appproto", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Priority", "priority", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Type", "type", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Sub Type", "subtype", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Message", "message", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_NONE);
        //temp[++idx] = new FlexRecordField("Request", "request", FlexRecordField.TYPE_STRING, FlexRecordField.SUB_TYPE_NONE);
        return temp;
    }

    public String getInsertFieldTableSQL(int id, String table) {
        StringBuffer temp = new StringBuffer("insert into " + table + " values (" + id + "");
        temp.append(",'");
        temp.append(NetFSEUtil.convertLongToIP(getSrcIP()) + "'");
        temp.append(",");
        temp.append(srcPort + "");
        temp.append(",'");
        temp.append(NetFSEUtil.convertLongToIP(dstAddr) + "'");
        temp.append(",");
        temp.append(dstPort);
        temp.append(",");
        temp.append("'" + getTimestamp().toString() + "'");
        temp.append(",");
        temp.append(duration + "");
        temp.append(",");
        temp.append("'" + srcName + "'");
        temp.append(",");
        temp.append(srcPackets + "");
        temp.append(",");
        temp.append(srcBytes + "");
        temp.append(",");
        temp.append("'" + dstName + "'");
        temp.append(",");
        temp.append(dstPackets + "");
        temp.append(",");
        temp.append(dstBytes + "");
        temp.append(",");
        temp.append(priority + "");
        temp.append(",");
        temp.append(eventType + "");
        temp.append(",");
        temp.append(eventCode + "");
        temp.append(",");
        temp.append(rule + "");
        temp.append(",");
        temp.append(httpStatus + "");
        temp.append(",");
        temp.append("'" + user + "'");
        temp.append(",");
        temp.append("'" + resource + "'");
        temp.append(",");
        temp.append("'" + format + "'");
        temp.append(",");
        temp.append("'" + fwType + "'");
        temp.append(",");
        temp.append("'" + appProto + "'");
        temp.append(",");
        temp.append("'" + type + "'");
        temp.append(",");
        temp.append("'" + subType + "'");
        temp.append(",");
        temp.append("'" + request + "'");
        temp.append(",");
        temp.append("'" + message + "'");
        temp.append(")");
        return temp.toString();
    }
    
    
    
}